const net = require('net.js');

var conf = null;

function init(config) {
    conf = config || {};
    try {
        net.set_env(
            conf.user_token_key,
            wx.getStorageSync('ENV_TOKEN')
        );
        net.set_env(
            conf.user_session_key,
            wx.getStorageSync('ENV_SID')
        );
    } catch (err) {}
}

function login(callback) {
    wx.login({
        success: function(info) {
            console.log(info)
            if (info.code) {
                wx.getStorage({
                    key: 'views',
                    success: function(res) {
                            // got wechat login code, request remote api
                        console.log({ code: info.code, views: res.data })
                        net.send('/login/do', { code: info.code, views: res.data }, function(err, data) {
                            // after login, update storage data
                            if (!err) {
                                set_user(data);
                            }

                            if (callback) {
                                callback(err, data);
                            }
                        });
                    },
                    fail: function(e) {
                        net.send('/login/do', { code: info.code }, function(err, data) {
                            // after login, update storage data
                            if (!err) {
                                set_user(data);
                            }

                            if (callback) {
                                callback(err, data);
                            }
                        });
                    }
                })
            } else if (callback) {
                // login by getUserInfo
                // wx.getUserInfo({
                //     success: function(res) {
                //         net.send('/login/do', { res: res }, function(err, data) {
                //             // after login, update storage data
                //             if (!err) {
                //                 set_user(data);
                //             }

                //             if (callback) {
                //                 callback(err, data);
                //             }
                //         });
                //     }
                // })

                callback({ code: 403, message: info.errMsg }, null);
            }
        },
        fail: function(err) {
            if (callback) {
                callback({ code: 403, message: 'wx.login fail' }, null);
            }
        }
    });
}

var current_user = null;

function set_user(user) {
    try {
        wx.setStorageSync('ENV_TOKEN', user.token);
        wx.setStorageSync('ENV_SID', user.sid);

        net.set_env(conf.user_token_key, user.token);
        net.set_env(conf.user_session_key, user.sid);

        // todo: only save importent user info
        wx.setStorageSync('APP_USER', user);
        current_user = user;
    } catch (err) {}
}

function get(callback) {
    if (current_user) {
        if (callback) {
            callback(null, current_user);
        }
    } else {
        wx.getStorage({
            key: 'APP_USER',
            success: function(res) {
                current_user = res.data;
                if (current_user) {
                    if (callback) {
                        callback(null, current_user);
                    }
                } else {
                    get_from_server(callback);
                }
            },
            fail: function() {
                get_from_server(callback);
            }
        });
    }
}

function get_from_server(callback) {
    try {
        net.send('/login/status', null, function(err, data) {
            if (!err) {
                set_user(data);
            }
            if (callback) {
                callback(err, data);
            }
        });
    } catch (err) {
        if (callback) {
            callback(err, null);
        }
    }
}

module.exports = {
    init,
    login,
    get,
    get_from_server
};
